/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.lib.client.control;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import java.util.ArrayList;
import java.util.List;
import org.lucava.boatskeeper.client.event.AuthEvent;
import org.lucava.lib.client.event.InitEvent;
import org.lucava.lib.client.event.InitHandlersEvent;
import org.lucava.lib.client.layout.LayoutManager;
import org.lucava.lib.client.service.SessionService;
import org.lucava.lib.client.service.SessionServiceAsync;

/**
 *
 * @author glenn
 */
public class FrontControl {

    final private EventBus eventBus;
    List<Control> controllers;
    private LayoutManager layoutManager;
    private Boolean secure;

    public FrontControl(EventBus eventBus) {

        this.eventBus = eventBus;
        this.controllers = new ArrayList<Control>();

    }

    public void run() {
        eventBus.fireEvent(new InitHandlersEvent());
        eventBus.fireEvent(new InitEvent());

        if (isSecure()) {
            eventBus.fireEvent(new AuthEvent());
        } else {
            getLayoutManager().render();
        }
    }

    public List<Control> getControllers() {
        return controllers;
    }

    public void addController(Control controller) {
        controller.setEventBus(eventBus);
        controller.init();
        controller.setLayoutManager(getLayoutManager());

        this.controllers.add(controller);
    }

    public EventBus getEventBus() {
        return eventBus;
    }

    public LayoutManager getLayoutManager() {
        if (layoutManager == null) {
            layoutManager = new LayoutManager();
        }
        return layoutManager;
    }

    public void setLayoutManager(LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
    }

    public Boolean isSecure() {
        return this.secure;
    }

    public void setSecure(Boolean secure) {
        this.secure = secure;
    }
}
